﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.Entity;
using WCS.Controls.RunLog;
using WCS.Utils;

namespace WcsTask.Routes
{
    public class RouteSet
    {
        private RouteSet() { }

        private static readonly RouteSet _routeSet = new RouteSet();

        public static RouteSet routeSet
        {
            get
            {
                return _routeSet;
            }
        }
        static SqlSugarDapper sqlSugarDapper = SqlSugarDapper.GetDbContext();

        /// <summary>
        /// WCS路径集合
        /// </summary>
        public List<WcsRoute> _wcsRoute;

        /// <summary>
        /// 获取初始化
        /// </summary>
        public void RouteInit()
        {
            _wcsRoute = sqlSugarDapper.DB.Queryable<WcsRoute>().ToList();
        }

        public List<string> routerRmember = new List<string>();

        /// <summary>
        /// 目标位置
        /// </summary>
        public string _targetStation;

        /// <summary>
        /// 获取下一段路径
        /// </summary>
        /// <param name="startLocation">起始位置</param>
        /// <param name="endLocation">目标位置</param>
        /// <param name="endLocation">制定下一个目标站台</param>2004
        /// <returns>下一个路径的信息</returns>
        public WcsRoute RouteQuery(string startLocation, string endLocation, List<string> formulateStation)
        {
            //起始位置不能等于目标位置
            if (startLocation == endLocation)
            {
                RunLog.runLog.Warning($"起始位置【{startLocation}】等于目标位置【{endLocation}】");
                return null;
            }
            List<WcsRoute> nextStationList = null;

            //根据目标位置位置获取上一段路径的集合
            nextStationList = _wcsRoute.FindAll(p => p.Next_Station == endLocation && formulateStation.Contains(p.Start_Station));
            if (nextStationList.Count == 0)
            {
                nextStationList = _wcsRoute.FindAll(p => p.Next_Station == endLocation);
            }

            //判断上一段路径是否为空
            if (nextStationList.Count == 0)
            {
                return null;
            }

            if (_targetStation == null)
            {
                _targetStation = endLocation;
            }

            //路径中起始位置是否可直接到达目标位置
            if (nextStationList.FindAll(t => t.Start_Station == startLocation).Count == 0)
            {
                //如果不能直接达到目标位置  开始循环
                foreach (var item in nextStationList.FindAll(n => n.Finish != "Finish" || n.Next_Station == _targetStation))
                {
                    if (!routerRmember.Contains(item.Start_Station))
                    {
                        routerRmember.Add(endLocation);
                        var routeSuccess = RouteQuery(startLocation, item.Start_Station, formulateStation);
                        if (routeSuccess != null)
                        {
                            //递归执行并返回，把当前对象的起始位置做参数
                            return routeSuccess;
                        }
                    }
                }
            }
            else
            {
                //可直接到达 返回当前路径
                if (formulateStation.Count != 0)
                {
                    var formulate = nextStationList.Find(t => t.Start_Station == startLocation && formulateStation.Contains(t.Next_Station));
                    if (formulate == null)
                    {
                        //根据目标位置位置获取上一段路径的集合
                        return nextStationList.Find(t => t.Start_Station == startLocation);
                    }
                    return formulate;
                }
                return nextStationList.Find(t => t.Start_Station == startLocation && (t.Finish != "Finish" || t.Next_Station == _targetStation));
            }
            return null;
        }
    }
}
